<template>
  <div class="todo-header">
<!--简写： @keydown.enter="$emit('addTodo',($event.target as HTMLInputElement).value)"   -->
<!--  keydown.enter vue3中取消了键盘事件的数字，改为固定的键盘英文  -->
    <input
        type="text"
        placeholder="请输入你的任务名称，按回车键确认"
        @keydown.enter="add"
    />
  </div>
</template>

<script lang="ts">
import {defineComponent} from "vue";
export default defineComponent({
  name: "Header"
})
</script>

<script lang="ts" setup>
//1.自定义事件直接接收(已经接收到了),可直接在模板中$emit使用,因为模板中是插值语法,存在组件实例,能找到$emit
// defineEmits<{
//   (emit:'addTodo',thing:string):void
// }>()

//defineEmits接收后得到一个emit函数,可以以函数接收并调用emit函数书写额外逻辑
const emit = defineEmits<{(emit:'addTodo',thing:string):void}>()

// 2.自定义事件接收后书写逻辑
const add = (e:Event)=>{
  emit('addTodo',(e.target as HTMLInputElement).value);
  //清空显示的添加数据
  //  对 e.target  目标元素进行断言 书写类型为 HTMLInputElement
  (e.target as HTMLInputElement).value = ''
}
</script>

<style scoped>
.todo-header input {
  width: 560px;
  height: 28px;
  font-size: 14px;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 4px 7px;
}

.todo-header input:focus {
  outline: none;
  border-color: rgba(82, 168, 236, 0.8);
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075),
  0 0 8px rgba(82, 168, 236, 0.6);
}
</style>
